/******************************************************************************
//             INTEL CORPORATION PROPRIETARY INFORMATION
//  This software is supplied under the terms of a license agreement or
//  nondisclosure agreement with Intel Corporation and may not be copied
//  or disclosed except in accordance with the terms of that agreement.
//    Copyright(c) 2000 - 2001 Intel Corporation. All Rights Reserved.
//
//  VSS: 
//     $Workfile: $
//     $Revision: $
//     $Date:     $
//     $Archive:  $
//
//  Description:
//      Intel(R) Integrated Performance Primitives - JPEG Helper file.
//      This file contains statistic functions to collect FREG of 
//      occurrrence of symbol V (Baseline)
//
//  History:
//     Date         Author        Changes
//     2001/2/1     Jerry Hu      Created (Version 0.2, K1, k4 in T.81)
//
********************************************************************************/
#include "_ippjpeg.h"
#include "ijehufsh.h"

/******************************************************************************
// Name:             ippiGetHuffmanFREQ_JPEG_16s
// Description:      Collect Huffman run length frequency.
// Input Arguments: 
//             pSrc: Identifies source coefficient data in 8x8.
//          pDCPred: Identifies the previous DC value of prediction.
//
// Output Arguments:
//          pDstDCFREQ: Identifies frequency for DC coefficient. Its length shall be 258.
//          pDstACFREQ: Identifies frequency for AC coefficient. Its length shall be 258.
//          pDCPred: Identifies the current DC value of prediction.
//
// Remarks:      
//                   1. The start address of "pSrc" shall be aligned at 4byte boundary.
//                   2. It is better to do this alignement at 32byte boundary to optimize 
//                      D-Cache accessing.
//                   3. *pDCPred shall be 0 in initial or after each restart interval.
//
// Returns:         
//    IPP_STATUS_OK: Succeeds.
// IPP_STATUS_ERROR: Decoding error.
******************************************************************************/
IPPFUN(IppStatus, ippiGetHuffmanFREQ_JPEG_16s) (const Ipp16s *pSrc, 
                                                int          *pDstDCFREQ,
                                                int          *pDstACFREQ,
                                                Ipp16s       *pDCPred)
{
    int    rrrr;
    int    ssss;
    int    rs;
    int    data;
    int    i;

    _IPP_CHECK_ARG((NULL!=pSrc)&&(NULL!=pDstDCFREQ)&&(NULL!=pDstACFREQ));
    _IPP_CHECK_ARG(NULL!=pDCPred);
    /*
    // Encode DC Coefficient
    */

    data = pSrc[0] - *pDCPred;
    _CSIZE(ssss, data);
    *pDCPred = pSrc[0];

    /* Encode Size of DC Coefficient */
    pDstDCFREQ[ssss]++;

    /*
    // Encode AC Coefficient(s)
    */

    rrrr = 0;

    for (i=1; i<64; i++) {
        data = pSrc[_nIppIZigZagIndex[i]];
        if (0==data) {
           rrrr++; /* Increment run-length of zeroes */
        } else {
            while (rrrr>15) {
                pDstACFREQ[0xf0]++;
                rrrr-=16;
            }

            _CSIZE(ssss, data);

            rs=(rrrr<<4)+ssss;

            pDstACFREQ[rs]++;
            rrrr = 0;
        } /* if (0==data) */
    }   /* for (i=1; i<IPPJPEG_BLOCKLEN; i++) */

    if (rrrr > 0) {
       /* Write EOB */
       pDstACFREQ[0x0]++;
    }

    return IPP_STATUS_OK;
}

/* EOF */
